--
-- Author: shunguo.chen
-- Date: 2019-06-10 18:11:41
--
-- LegendNewView 获得新神器界面
--

local AvatarFactory = app:getClass("AvatarFactory")
local Armature = app:getClass("Armature")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountNewScene

local NEW_GET_STR = L("LegendNew")

function ClassRef:init()
    self._ui["Panel/Panel_bg/Image_1"]:setContentSize(display.size)

    self._ui.Button_1:addClickEventListener(function()
        self._ui.Button_1:hide()
        self._ui.Button_2:hide()
        local pc = app:getInst("PhotoController")
        pc:snapshotNode(self)
        local act = cca.seq({cca.delay(0), cca.callFunc(function()
            -- UIHandler:getPopupUi():removeUIByKey(ClassRef:getViewName())
            pc:showPhotoViewShare()
            self._ui.Button_1:show()
            self._ui.Button_2:show()
        end), })

        self._ui.Button_1:runAction(act)
    end)
    self._ui.Button_2:addClickEventListener(function()
        executeFunc(self._onCloseView, self)
    end)

    self._starTimeline = self._ui["Panel/light"]:getTimeline()
    self._starTimeline:pause()

end

function ClassRef:show()
    self:setVisible(true)
    self._root:setVisible(false)

    local params = {
        armatureName = "getride",
        path = Res.MountNewEfcPath
    }
    local armature = Armature.create(params)
    armature:setPosition(display.cx, display.cy)
    self:addChild(armature, 2)

    armature:play("getride", 1, function(sender)
            sender:removeFromParent()
        end)
    armature:onFrameEvent(function(sender, name)
            if name == "all" then
                self._root:setVisible(true)

                -- create legend avatar
                local param = self._data:getShowAvatarParam()
                local legend = Armature.create(param)
                if not legend then return end
                legend:setScale(param.scale)
                legend:setRotation(param.rotation)
                legend:playDefault()
                self._ui.mount:addChild(legend)

                local width, height = self._ui.mount:getContentSize2()
                local classId = self._data:getClass()
                local offsetPos, offsetPosEx = self._data:getAvatarOffsetPos()
                legend:setPosition(width/2 + offsetPos[1], height/2 + offsetPos[2])
                if classId == 4 then
                    local legendEx = Armature.create(param)
                    legendEx:setScale(param.scale)
                    legendEx:setRotation(param.rotationEx)
                    legendEx:setPosition(width/2 + offsetPosEx[1], height/2 + offsetPosEx[2])
                    legendEx:playDefault()
                    self._ui.mount:addChild(legendEx)
                end
            end
        end)

    AM.playSoundById(5153)
end

-- legend LegendVo
function ClassRef:refresh(legend, tipStr)
    self._data = legend
    self._ui.title:setString(string.format(NEW_GET_STR, legend:getName()))
    if tipStr then
        self._ui.text:setVisible(true)
        self._ui.text:setString(tipStr)
    else
        self._ui.text:setVisible(false)
    end
end

return ClassRef
